package com.wyj.wuliwuli.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wyj.wuliwuli.common.AjaxRequest;
import com.wyj.wuliwuli.common.AjaxResult;
import com.wyj.wuliwuli.common.PageParam;
import com.wyj.wuliwuli.entity.WuliNotice;
import com.wyj.wuliwuli.entity.mapper.WuliNoticeMapper;
import com.wyj.wuliwuli.service.WuliNoticeService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Optional;

/**
 * @Version 1.0
 * @Description
 */
@Service
@RequiredArgsConstructor
public class WuliNoticeServiceImpl implements WuliNoticeService {

    private final WuliNoticeMapper wuliNoticeMapper;


    /**
     * 分页查询
     *
     * @param ajaxRequest
     * @return
     */
    @Override
    public AjaxResult page(AjaxRequest<PageParam> ajaxRequest) {
        JSONObject params = ajaxRequest.getParams();
        if (params == null) {
            return AjaxResult.fail("接口参数不能为空");
        }
        Long userId = params.getLong("userId");
        if (userId == null) {
            return AjaxResult.fail("userId不能为空");
        }
        PageParam pageParam = ajaxRequest.getData();
        Pageable pageable = PageRequest.of(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getDirection(), pageParam.getProperty());
        Page<WuliNotice> page = wuliNoticeMapper.findByIsReadIsNotAndUser_Id(1, userId, pageable);
        return AjaxResult.ok(page);
    }

    /**
     * 根据ID查询
     *
     * @param id 通知ID
     * @return
     */
    @Override
    public AjaxResult getById(Long id) {
        if (id == null) {
            return AjaxResult.fail("参数id不能为空");
        }
        Optional<WuliNotice> notice = wuliNoticeMapper.findById(id);
        return notice.map(AjaxResult::ok).orElse(AjaxResult.fail("通知不存在或已删除"));
    }

    /**
     * 根据ID已读
     *
     * @param id 通知ID
     * @return
     */
    @Override
    public AjaxResult read(Long id) {
        if (id == null) {
            return AjaxResult.fail("参数id不能为空");
        }
        Optional<WuliNotice> optional = wuliNoticeMapper.findById(id);
        if (optional.isPresent()) {
            WuliNotice notice = optional.get();
            if (notice.getIsRead() == null || notice.getIsRead() == 0) {
                notice.setIsRead(1);
                wuliNoticeMapper.save(notice);
            }
        } else {
            return AjaxResult.fail("通知不存在或已删除");
        }
        return AjaxResult.of();
    }

    /**
     * 已读全部
     *
     * @param userId 用户ID
     * @return
     */
    @Override
    public AjaxResult readAll(Long userId) {
        List<WuliNotice> noticeList = wuliNoticeMapper.findByUser_IdAndIsReadIsNot(userId, 1);
        if (!CollectionUtils.isEmpty(noticeList)) {
            for (WuliNotice notice : noticeList) {
                notice.setIsRead(1);
                wuliNoticeMapper.save(notice);
            }
        }
        return AjaxResult.of();
    }
}
